<?php

namespace Mvc\config\ArrayAccess;
/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-21
 * Time: 17:29
 * @description: 实现 ArrayAccess 接口
 * @Author: yfl
 * @QQ 554665488
 */
class  Config implements \ArrayAccess
{
    protected $path;//配置文件路径

    protected $configs = array();//保存加载的文件到类的属性

    public function __construct($path)
    {
        $this->path = $path;
    }


    public function offsetSet($offset, $value)
    {
        // TODO: Implement offsetSet() method.
        throw  new \Exception('cannot write file config');

    }

    //获得配置文件加载到类的属性中
    public function offsetGet($offset)
    {
        // TODO: Implement offsetGet() method.
        if (empty($this->configs[$offset])) {
            $file_path = $this->path .CONFIG_PATH. $offset . '.php';
            $config = require "{$file_path}";
            return $this->configs[$offset] = $config;
        }
    }

    public function offsetExists($offset)
    {
        // TODO: Implement offsetExists() method.
        return isset($this->config[$offset]);
    }

    public function offsetUnset($offset)
    {
        // TODO: Implement offsetUnset() method.
        unset($this->configs[$offset]);
    }

}